package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
@Slf4j
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private NursingProjectPlanServiceImpl nursingProjectPlanServiceImpl;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CACHE_KEY = "nursingPlan:all";


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        // 1.根据id查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        // 2.根据护理计划id查询关联的所有护理项目
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);

        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        // 批量保存护理项目计划关系
        if (projectPlans.isEmpty()) {

            return this.save(nursingPlan) ? 1 : 0;
        }
        this.save(nursingPlan);
        projectPlans.forEach(nursingProjectPlan -> {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
        });
        redisTemplate.delete(CACHE_KEY);
        return nursingProjectPlanServiceImpl.saveBatch(projectPlans) ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);

        if (dto.getProjectPlans() != null && !dto.getProjectPlans().isEmpty()) {
            nursingProjectPlanServiceImpl
                    .remove(new LambdaQueryWrapper<NursingProjectPlan>()
                            .eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            dto.getProjectPlans().forEach(nursingProjectPlan ->
                    nursingProjectPlan.setPlanId(nursingPlan.getId()));
            nursingProjectPlanServiceImpl.saveBatch(dto.getProjectPlans());
        }

        redisTemplate.delete(CACHE_KEY);
        return this.updateById(nursingPlan) ? 1 : 0;

    }


    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = selectNursingPlanById(id);
        if (nursingPlanVo.getStatus() == 1) {
            throw new RuntimeException("该护理计划正在使用中，请勿删除");
        }
        if (nursingPlanVo.getProjectPlans().isEmpty()) {
            return removeById(id) ? 1 : 0;
        }
        nursingProjectPlanServiceImpl
                .remove(new LambdaQueryWrapper<NursingProjectPlan>()
                        .eq(NursingProjectPlan::getPlanId, id));

        redisTemplate.delete(CACHE_KEY);
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlan> listAll() {

        List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY);
        if (list != null) {
            log.info("缓存命中");
            return list;
        }
        list = list(new LambdaQueryWrapper<NursingPlan>().eq(NursingPlan::getStatus, 1));
        redisTemplate.opsForValue().set(CACHE_KEY, list);
        return list;
    }
}
